package com.zifeng.filter;

import com.zifeng.utils.RefreshTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


@Component
public class TokenFilter implements GlobalFilter, Ordered {

    @Autowired
    private RefreshTokenUtil refreshTokenUtil;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        refreshTokenUtil.refreshToken(request);

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -10; // 优先级要高于其他过滤器
    }
}
